package com.sanjiahantang.youjiaclient.module.common.models.impl;

import android.content.Context;

import com.google.gson.Gson;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.http.BaseDelegate;
import com.sanjiahantang.youjiaclient.http.OkHttpClientManager;
import com.sanjiahantang.youjiaclient.module.common.beans.ScreenAdBean;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.common.models.IMainModel;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zy on 2017/8/31.
 */

public class MainModel implements IMainModel {
    public static final MediaType JSON =  MediaType.parse("application/json;charset=UTF-8");
//    public static final MediaType JSON =  MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8");

    @Override
    public void getPersonalInfo(final Context context, Map<String, String> params, final CallBack<StringBean> callback) {
//        String key = (String) SPUtils.get(context, "key", "");
//        params.put(SPConfig.KEY, key);
//        OkHttpClientManager.postAsyn(context, ConfigValue.InitPersonalInfo, params, new BaseDelegate.ResultCallback<StringBean>() {
//            @Override
//            public void onError(Request request, Object tag, Exception e) {
//
//            }
//
//            @Override
//            public void onResponse(StringBean response, Object tag) {
//                callback.onSuccess(response,context);
//            }
//        });

        Temp t = new Temp();
        t.page = 0;
        t.pageSize = 10;
        Item item = new Item();
        item.phone = "15510563729";
        t.param = item;
        t.token = "296554c40d2d439249b9c578c";
        final String json = new Gson().toJson(t);
        final Map<String,String> param = new HashMap<>();
        param.put("phone","18822185396");
        param.put("password","111111");
//        final String json = new Gson().toJson(param);
        /*OkHttpClientManager.postAsyn(context,"http://172.16.60.12:8000/user/phone/customer/loginForPassword",param,new BaseDelegate.ResultCallback<String>() {

                    @Override
                    public void onError(Request request, Object tag, Exception e) {
//                callBack.onFailure(e.getMessage());
                        Log.e("bbbb",e.getMessage());
                        Utils.showToast(context,"error:" + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, Object tag) {
//                    callBack.onSuccess(response,context);
                        Log.e("aaaa", response);
                        Utils.showToast(context,"response:" + response);
                    }
                });*/


        /*new Thread(){
            @Override
            public void run() {
                super.run();
                String url = "http://172.16.60.151:8000/case/phone/role/searchCaseInfo";
                Map<String,Object> params = new HashMap<>();
                params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
                params.put(KEY_PAGE,String.valueOf(0));
                params.put(KEY_PAGE_SIZE,String.valueOf(10));
                Map<String,String> map = new HashMap<>();
                map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
                params.put(KEY_PARAM,map);
                try {
                    String response = httpPost(url, new Gson().toJson(params));
                    System.out.println(response);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();*/


    }

    @Override
    public void loadScreenAdvisement(final Context context, final CallBack<ScreenAdBean> callBack) {
        OkHttpClientManager.postJson(context,ConfigValue.API_URL + "/system/advertisement/phone/findScreenAdPhoneForCustomer",new HashMap<String, Object>(),new BaseDelegate.ResultCallback<ScreenAdBean>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(ScreenAdBean response, Object tag) {
                callBack.onSuccess(response,context,tag);
            }
        });
    }

    @Override
    public void bindRole(final Context context, Map<String, String> param, final CallBack<StringBean> callBack) {
        OkHttpClientManager.postAsyn(context,ConfigValue.API_URL + "/user/phone/customer/bindPartner",
                param,new BaseDelegate.ResultCallback<StringBean>() {

            @Override
            public void onError(Request request, Object tag, Exception e) {
                callBack.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(StringBean response, Object tag) {
                callBack.onSuccess(response,context,tag);
            }
        });
    }

    public static String httpGet(String url) throws IOException {
        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = httpClient.newCall(request).execute();
        return response.body().string(); // 返回的是string 类型，json的mapper可以直接处理
    }

    public static String httpPost(String url, String json) throws IOException {
        OkHttpClient httpClient = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = httpClient.newCall(request).execute();
        return response.body().string();
    }

    public <T> Map<String,Object> beanToMap(T obj){
        final Map<String, Object> resultMap = new HashMap<>();
        Class<?> aClass = obj.getClass();
        for (Field field : aClass.getDeclaredFields()) {
            String name = field.getName();
            if(!(name.indexOf("serialVersionUID") != -1) && !(name.indexOf("this") != -1)) {
                field.setAccessible(true);
                try {
                    Object o = field.get(obj);
                    resultMap.put(field.getName(), o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return resultMap;
    }

    private class Temp{
        String token;
        int pageSize;
        int page;
        Item param;
    }
    private class Item{
        String phone;
    }
}
